package com.example.a2032.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.example.a2032.R;
import com.example.a2032.Utils.RxBus;
import com.example.a2032.Utils.SharedPreferenceUtils;
import com.example.a2032.Utils.Utils;
import com.example.a2032.entity.ActionData;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WatchFaceShowFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WatchFaceShowFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    @BindView(R.id.iv_watchFace)
    ImageView ivWatchFace;

    // TODO: Rename and change types of parameters
    private int mParam1;


    public WatchFaceShowFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment WatchFaceShowFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static WatchFaceShowFragment newInstance(int position) {
        WatchFaceShowFragment fragment = new WatchFaceShowFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_watch_face_show, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;

    }

    private void init() {
        int[] resIds = Utils.getResourcesImageArray(getContext(), R.array.watchface_img_array);
        ivWatchFace.setImageResource(resIds[mParam1]);
    }

    private void changeFace() {
        RxBus.getInstance().sendAction(ActionData.ChangeWatchStyle);
        SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_WatchFace, mParam1);
        getActivity().finish();
    }

    @OnClick(R.id.iv_watchFace)
    public void onViewClicked() {
        changeFace();
    }
}